package com.ruoyi.project.system.controller;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysOption;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.project.system.service.ISysOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysRecord;
import com.ruoyi.project.system.service.ISysRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author theme
 * @date 2020-05-27
 */
@Api(description = "报名记录")
@RestController
@RequestMapping("/system/record")
public class SysRecordController extends BaseController {
    @Autowired
    private ISysRecordService sysRecordService;
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysOptionService sysOptionService;

    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation("查询【请填写功能名称】列表")
    @PreAuthorize("@ss.hasPermi('system:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRecord sysRecord) {
        startPage();
        List<SysRecord> list = sysRecordService.selectSysRecordList(sysRecord);
        return getDataTable(list);
    }

    @ApiOperation("查询填写过信息的列表")
    @PreAuthorize("@ss.hasPermi('system:record:record')")
    @GetMapping("/recordListMobileUser")
    public TableDataInfo record(SysRecord sysRecord) {
        /*List<SysRecord> sysRecords = sysRecordService.selectSysRecordListMobileUser();
        List<SysMobileUser> list = new ArrayList<>();
        for (int i = 0; i < sysRecords.size(); i++) {
            SysRecord sysRecord = sysRecords.get(i);
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysRecord.getMobileUserId());
            if (null != sysMobileUser) {
                Long mobileUserId = sysMobileUser.getMobileUserId();
                List<SysRecord> records = sysRecordService.selectSysRecordByMobileUserId(mobileUserId);
                List<SysRecord> recordList = new ArrayList<>();
                for (int j = 0; j < records.size(); j++) {
                    Long optionId = records.get(j).getOptionId();
                    SysOption sysOption = sysOptionService.selectSysOptionById(optionId);
                    records.get(j).setSysOption(sysOption);
                    recordList.add(records.get(j));
                }
                sysMobileUser.setRecords(recordList);
                list.add(sysMobileUser);
            }
        }*/
        /*startPage();
        List<SysMobileUser> list = new ArrayList<>();
        //查userid
        List<SysRecord> recordMobile = sysRecordService.selectMobileUserId();
        List<SysOption> optionList = sysOptionService.selectOptionList();
        List<SysRecord> recordList = sysRecordService.selectRecordByMobileUserId(null);
        Map<Long, List<SysRecord>> collect = recordList.stream().collect(Collectors.groupingBy(SysRecord::getMobileUserId));
        for (SysRecord sysRecordMobile : recordMobile) {
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(sysRecordMobile.getMobileUserId());
            List<SysRecord> records = collect.get(sysRecordMobile.getMobileUserId());
            for (SysRecord record : records) {
                Long optionId = record.getOptionId();
                for (SysOption sysOption : optionList) {
                    Long optionId1 = sysOption.getOptionId();
                    if (optionId.equals(optionId1)) {
                        record.setSysOption(sysOption);
                    }
                }
            }
            Map<String, List<SysRecord>> collectUuid = records.stream().collect(Collectors.groupingBy(SysRecord::getUuid));

            List<SysRecord> records1= new LinkedList<>();
            for (Map.Entry<String, List<SysRecord>> m : collectUuid.entrySet()) {
                records1 = collectUuid.get(m.getKey());
            }
            sysMobileUser.setRecords(records1);
//            sysMobileUser.setRecordUuid(collectUuid);
            list.add(sysMobileUser);
        }*/

        Integer PAGE_NUM = ServletUtils.getParameterToInt(TableSupport.PAGE_NUM);
        Integer PAGE_SIZE = ServletUtils.getParameterToInt(TableSupport.PAGE_SIZE);

        int scol = PAGE_SIZE * (PAGE_NUM - 1);
        int ecol = PAGE_SIZE * PAGE_NUM;

        sysRecord.setStart(scol);
        sysRecord.setEnd(ecol);

        List<SysMobileUser> list = new ArrayList<>();
        //查userid
        List<SysOption> optionList = sysOptionService.selectOptionList();
        List<SysRecord> recordList = sysRecordService.selectRecordByMobileUserId(sysRecord);
        Map<String, List<SysRecord>> collect = recordList.stream().collect(Collectors.groupingBy(SysRecord::getUuid));

        Map<Long, List<SysRecord>> newCollect = new HashMap<>();
        for (Map.Entry<String, List<SysRecord>> m : collect.entrySet()) {
            long key = Long.parseLong(m.getKey());
            List<SysRecord> records = collect.get(m.getKey());
            newCollect.put(key, records);
        }

        TreeMap<String, List<SysRecord>> maps = new TreeMap<String, List<SysRecord>>(new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                // 降序排序
                long obj11 = Long.valueOf(obj1);
                long obj22 = Long.valueOf(obj2);
                if (obj11 == obj22) {
                    return 0;
                }
                if (obj11 > obj22) {
                    return -1;
                }
                if (obj11 < obj22) {
                    return 1;
                }
                return 0;
            }
        });
        maps.putAll(collect);
        for (Map.Entry<String, List<SysRecord>> m : maps.entrySet()) {
            List<SysRecord> records = maps.get(m.getKey());
            SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(records.get(0).getMobileUserId());
            for (SysRecord record : records) {
                Long optionId = record.getOptionId();
                for (SysOption sysOption : optionList) {
                    Long optionId1 = sysOption.getOptionId();
                    if (optionId.equals(optionId1)) {
                        record.setSysOption(sysOption);
                    }
                }
            }
            sysMobileUser.setRecords(records);
            list.add(sysMobileUser);
        }

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(sysRecordService.selectUuid(sysRecord).size());
        return rspData;
    }


    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation("获取【请填写功能名称】详细信息")
    @PreAuthorize("@ss.hasPermi('system:record:query')")
    @GetMapping(value = "/{mobileUserId}")
    public AjaxResult getInfo(@PathVariable("mobileUserId") Long mobileUserId) {
        return AjaxResult.success(sysRecordService.selectSysRecordById(mobileUserId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation("新增【请填写功能名称】")
    @PreAuthorize("@ss.hasPermi('system:record:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRecord sysRecord) {
        return toAjax(sysRecordService.insertSysRecord(sysRecord));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation("修改【请填写功能名称】")
    @PreAuthorize("@ss.hasPermi('system:record:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRecord sysRecord) {
        return toAjax(sysRecordService.updateSysRecord(sysRecord));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation("删除【请填写功能名称】")
    @PreAuthorize("@ss.hasPermi('system:record:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{mobileUserIds}")
    public AjaxResult remove(@PathVariable Long[] mobileUserIds) {
        return toAjax(sysRecordService.deleteSysRecordByIds(mobileUserIds));
    }
}
